﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <sys/timeb.h>

#include "common_func.h"

using namespace std;

int debug_print(const char *buf, int len)
{
	int i = 0;
    printf("in debug_print:\n*****************************\n");
	for(i = 0; i < len; i++)
	{
		printf("%02x ", buf[i] & 0xff);
		if(!((i+1) % 10)) printf("\n");
	}
	printf("size = %d\n*****************************\n", len);

    return 0;
}

int debug_printf(const char *buf, int len, const char *pmsg)
{
#ifdef _TEST_

	int i = 0;
    printf("in %s debug_print:\n*****************************\n",pmsg);
	for(i = 0; i < len; i++)
	{
		printf("%02x ", buf[i] & 0xff);
		if(!((i+1) % 10)) printf("\n");
	}
	printf("size = %d\n*****************************\n", len);
#endif
    return 0;
}

unsigned short Reversal16(unsigned short data)
{
	unsigned short reversal = (data & 0xff) << 8 | (data >> 8);
	return reversal;
}

//数组字节反转
void ReversalArray(unsigned char * data, int len)
{	
	char *pBak = new char[len];

	memcpy(pBak, data, len);

	int index = len -1;
	for ( int i = 0; i < len; i++ ,index--)
	{
		data[index] = pBak[i];
	}

	delete [] pBak;
	
}



//累加校验和
unsigned short sumCheck(unsigned char *data, int len)
{
	unsigned short sum = 0;
	for(int i  = 0 ; i < len; i++)
	{
		sum += data[i];
	}

	return sum;
}

unsigned char CheckSum(unsigned char *buf, int len)
{ 
    int i;
	unsigned char ret = 0;

    for( i = 0; i < len; i++ )
    {
        ret += buf[i];
    }
    return ret;
}

//科士达UPS使用
unsigned char kstar_length_sumCheck(const unsigned char  *data)
{
	unsigned char sum = 0;

    sum = data[0] & 0x0f ;
    sum += data[1] & 0x0f;
    sum += (data[1] >> 4) & 0x0f;
	
	sum = sum % 0x10;
	sum = ~sum; 
	sum += 1;

	sum = ((sum & 0x0f) << 4) & 0xf0;//返回低4bit位
	return sum; 
}

//电总协议使用
unsigned short kstar_msg_sumCheck(const unsigned char  *data, int len)
{
	unsigned short sum = 0;
	for(int i  = 0 ; i < len; i++)
	{
		sum += data[i];
	}

	sum = sum%0xffff;
	sum = ~sum; 
	sum += 1;
	
	return sum;
}

void StringReplace(string &strBase, const string &strSrc,const string &strDes)  
{  
    string::size_type pos = 0;  
    string::size_type srcLen = strSrc.size();  
    string::size_type desLen = strDes.size();  
    pos=strBase.find(strSrc, pos);   
    while ((pos != string::npos))  
    {  
        strBase.replace(pos, srcLen, strDes);  
        pos=strBase.find(strSrc, (pos+desLen));  
    }  
}  

//转义字符添加  : 0x7e --> 0x7F 0x01, 0x7F --> 0x7F, 0x72
void EncodeEscape(const string srcStr, string &destStr)
{
    string strTemp = srcStr;
    string::size_type curPos = 0;

    while((curPos = strTemp.find_first_of("\x7e\x7f", curPos)) != string::npos)
    {
        char chTemp = strTemp.at(curPos);

        if(0x7e == chTemp)
        {
            strTemp.replace(curPos, 1, "\x7f\x01");
        }
        else if(0x7f == chTemp)
        {
            strTemp.replace(curPos, 1, "\x7f\x02");
        }

        curPos++;
    }

	destStr = strTemp;

}

//转义字符解析  :     0x7F 0x01 --> 0x7e, 0x7F, 0x72 --> 0x7F
void DecodeEscape(const string srcStr, string &destStr)
{
    string strTemp = srcStr;
    string::size_type curPos = 0;

    while((curPos = strTemp.find_first_of("\x7f", curPos)) != string::npos)
    {
        string chTemp = strTemp.substr(curPos, 2);

        if("\x7F\x01" == chTemp)
        {
            strTemp.replace(curPos, 2, "\x7e");
        }
        else if("\x7F\x02" == chTemp)
        {
            strTemp.replace(curPos, 2, "\x7f");
        }

        curPos++;
    }

	destStr = strTemp;

}










